package com.wbs.shop.nuby.view.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.commom.Key;
import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.utils.NubyLocalBroadcastManager;
import com.wbs.shop.nuby.view.listener.IWebView;

/**
 * Created by zhoucl on 2018/5/30 0030.
 */
public class BaseWebViewFragmnet extends BaseFragment {
    IWebView iWebView;
    private BroadcastReceiver mBroadcastReceiver;

    @Override
    protected int tellMeLayout() {
        return R.layout.fragment_webview;
    }

    public String getUrl() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            String url = arguments.getString(Key.URL);
            url = url + "?sid=" + arguments.getString(Key.SID);
            url = url + "&aid=" + arguments.getString(Key.AID);
            return url;
        }
        return null;
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        //第二步：同时给getHolder().setFormat(PixelFormat.TRANSPARENT);给SurfaceHolder设置
        iWebView = (IWebView) view;//.findViewById(R.id.webView)
        Bundle arguments = getArguments();
        if (arguments != null) {
            boolean isBroadcastReceiver = arguments.getBoolean(Key.IS_BROADCAST_RECEIVER);
            if (isBroadcastReceiver) {
                NubyLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
                mBroadcastReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        if (iWebView != null && Key.ACTION_LOGIN_SUCCESS.equals(intent.getAction())) {
                            iWebView.reload();
                        }
                    }
                };
                NubyLocalBroadcastManager.registerLoginSuccessReceiver(mBroadcastReceiver);
            }
        }
        loadUrl();
    }

    public void loadUrl() {
        String url = getUrl();
        Logger.e("loadUrl==>" + url);
        if (url != null)
            iWebView.loadUrl(url);
    }

    public void reload() {
        if (iWebView != null)
            iWebView.reload();
    }

    public void changSid(String sid, String aid) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            arguments.putString(Key.SID, sid);
            arguments.putString(Key.AID, aid);
            loadUrl();
        }
    }

    @Override
    public void onDestroy() {
        if (iWebView != null)
            iWebView.destroy();
        super.onDestroy();
    }

//    @Override
//    public void onPause() {
//        super.onPause();
//        if (iWebView != null) {
//            iWebView.pauseTimers();
//            iWebView.onHide();
//        }
//    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        if (iWebView != null) {
//            iWebView.resumeTimers();
//            iWebView.onShow();
//        }
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (iWebView != null) {
            iWebView.onActivityResult(requestCode, resultCode, data);
        }
    }
}
